package com.madvirus.review;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import com.madvirus.review.data.Review;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;

public class ReviewDetail extends Activity {

	private String imageLink;
	private String link;
	private TextView location;
	private TextView name;
	private TextView phone;
	private TextView rating;
	private TextView review;
	private ImageView reviewImage;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (imageLink != null && !imageLink.equals("")) {
				try {
					URL url = new URL(imageLink);
					URLConnection conn = url.openConnection();
					conn.connect();
					BufferedInputStream bis = new BufferedInputStream(
							conn.getInputStream());
					Bitmap bm = BitmapFactory.decodeStream(bis);
					bis.close();
					reviewImage.setImageBitmap(bm);
				} catch (IOException ex) {
				}
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.review_detail);

		name = (TextView) findViewById(R.id.name_detail);
		rating = (TextView) findViewById(R.id.rating_detail);
		location = (TextView) findViewById(R.id.location_detail);
		phone = (TextView) findViewById(R.id.phone_detail);
		review = (TextView) findViewById(R.id.review_detail);
		reviewImage = (ImageView) findViewById(R.id.review_image);

		RestaurantFinderApplication application = (RestaurantFinderApplication) getApplication();
		Review currentReview = application.getCurrentReview();

		link = currentReview.link;
		imageLink = currentReview.imageLink;
		name.setText(currentReview.name);
		rating.setText(currentReview.rating);
		location.setText(currentReview.location);
		review.setText(currentReview.content);

		if (currentReview.phone != null && !currentReview.phone.equals("")) {
			phone.setText(currentReview.phone);
		} else {
			phone.setText("N/A");
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}

}
